package patrol.mobile.com.mobilepatrol.utils;

import android.app.DatePickerDialog;
import android.content.Context;
import android.widget.DatePicker;

import java.util.Calendar;

import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.view.CallBack;

/**
 * Created by z on 2018/9/8.
 * Des: 日期选择工具
 */

public class DatePickerUtil {
    private static DatePickerUtil datePickerUtil;
    private CallBack<DateBean> callBack;

    public static DatePickerUtil getInstance(Context context) {
//        if (datePickerUtil == null) {
        datePickerUtil = new DatePickerUtil(context);
//        }
        return datePickerUtil;
    }

    private DatePickerUtil(Context context) {
        Calendar c = Calendar.getInstance();
        int mYear = c.get(Calendar.YEAR);
        int mMonth = c.get(Calendar.MONTH);
        int mDay = c.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog datePickerDialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                DateBean dateBean = new DateBean();
                dateBean.setmYear(year);
                String yuefen = (month + 1) + "";
                dateBean.setmMonth(yuefen);
                dateBean.setmDay(dayOfMonth);
                String tianshu = dayOfMonth + "";

                if (dayOfMonth < 10) {
                    tianshu = "0" + dayOfMonth;
                }
                if (month < 9) {
                    yuefen = "0" + (month + 1);
                }
                dateBean.setmDate(year + "-" + yuefen + "-" + tianshu);
                callBack.onBack(dateBean);
            }
        }, mYear, mMonth, mDay);
        datePickerDialog.show();
    }

    public void setCallBack(CallBack<DateBean> callBack) {
        this.callBack = callBack;
    }
}
